//
//  MessageViewController.m
//  tristana
//
//  Created by 李康 on 2021/1/5.
//  Copyright © 2021 李康. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageTableViewCell.h"
#import "MessageDetailViewController.h"
#import "Otherthings.h"

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *mainTableView;

@property (strong, nonatomic) NSArray *dataArr;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"知识库";
    [self.view addSubview:self.mainTableView];
    
    self.dataArr = @[@{@"title":@"借款和欠款有什么区别",@"time":@"2021-01-02",@"image":@"pic1.jpg"},
                      @{@"title":@"民间借贷合同样本是怎样的",@"time":@"2020-12-28",@"image":@"pic2.jpg"},
                      @{@"title":@"借物条怎么写才是规范的",@"time":@"2020-12-27",@"image":@"pic3.jpg"},
                      @{@"title":@"共同之债法律规定是怎样的",@"time":@"2020-12-24",@"image":@"pic4.jpg"},
                      @{@"title":@"民间借贷律师费由败诉方承担的法律依据有哪些",@"time":@"2020-12-24",@"image":@"pic5.jpg"},
                      @{@"title":@"借钱怎么写借款条？",@"time":@"2020-12-22",@"image":@"pic6.jpg"},
                      @{@"title":@"朋友借钱不还，电话也不接怎么办",@"time":@"2020-12-20",@"image":@"pic7.jpg"},
                      @{@"title":@"小额欠款起诉流程",@"time":@"2020-12-17",@"image":@"pic8.jpg"},
                      @{@"title":@"债务追讨法律时效",@"time":@"2020-12-16",@"image":@"pic9.jpg"},
                      @{@"title":@"欠钱不还_不存在!还可以通过这些小绝招要钱!",@"time":@"2020-12-16",@"image":@"pic10.jpg"},
                      @{@"title":@"工程款讨债技巧有哪些",@"time":@"2020-12-15",@"image":@"pic11.jpg"},
                      @{@"title":@"催缴货款的方法介绍",@"time":@"2020-12-15",@"image":@"pic12.jpg"},
                      @{@"title":@"销售货款回笼技巧介绍",@"time":@"2020-12-13",@"image":@"pic13.jpg"},
                      @{@"title":@"法官谈讨债技巧",@"time":@"2020-12-11",@"image":@"pic14.jpg"},
                      @{@"title":@"电话收账技巧和信函收账技巧",@"time":@"2020-12-10",@"image":@"pic15.jpg"},
                      @{@"title":@"贸易追债连环计",@"time":@"2020-12-09",@"image":@"pic16.png"},
                      @{@"title":@"一箭双雕催款兵法",@"time":@"2020-12-20",@"image":@"pic17.jpg"},
                      @{@"title":@"要债的最好办法是怎样的",@"time":@"2020-12-08",@"image":@"pic18.jpg"},
                      @{@"title":@"债权人撤销权的期限有多久",@"time":@"2020-12-01",@"image":@"pic19.jpg"},
                      @{@"title":@"对于债权人豁免的债务协议怎么写",@"time":@"2020-12-01",@"image":@"pic20.jpg"}];
}

-(UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-TabbarHeight) style:UITableViewStylePlain];
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        [_mainTableView registerNib:[UINib nibWithNibName:@"MessageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MessageTableViewCell"];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
        __weak typeof (self)weakSelf = self;
        _mainTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
           [weakSelf loadMoreData];
        }];
        _mainTableView.mj_footer.automaticallyHidden = YES;
        MJRefreshBackNormalFooter *footer = (MJRefreshBackNormalFooter *)_mainTableView.mj_footer;
        [footer setTitle:@"上拉加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"释放加载更多" forState:MJRefreshStatePulling];
    }
    return _mainTableView;
}


#pragma mark -- <UITableViewDelegate,UITableViewDataSource>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *cellID = @"MessageTableViewCell";
    MessageTableViewCell *cell = (MessageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.dataDic = self.dataArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageDetailViewController *vc = [[MessageDetailViewController alloc]init];
    vc.contentUrl = [NSString stringWithFormat:@"%@%ld",@"text",indexPath.row+1];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)beginRefresh
{
        [_mainTableView.mj_header beginRefreshing];
  
}

-(void)endRefresh
{
    if ([_mainTableView.mj_header isRefreshing])
    {
        [_mainTableView.mj_header endRefreshing];
    }
    else if ([_mainTableView.mj_footer isRefreshing])
    {
        [_mainTableView.mj_footer endRefreshing];
    }
}

-(void)endLoadMore
{
    if ([_mainTableView.mj_footer isRefreshing]) {
        [_mainTableView.mj_footer endRefreshing];
    }
}

-(BOOL)isRefreshing{
    return [_mainTableView.mj_header isRefreshing];
}

//下拉刷新数据
-(void)refreshData{
    [self endRefresh];
    [_mainTableView reloadData];
}
//加载更多数据
-(void)loadMoreData{
    [self endLoadMore];
    [_mainTableView reloadData];
}



@end
